package com.spzx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.user.domain.Region;
import com.spzx.user.mapper.RegionMapper;
import com.spzx.user.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements IRegionService {
    @Autowired
    private RegionMapper regionMapper;

    /**
     * 查询地区信息列表
     *
     * @param region 地区信息
     * @return 地区信息
     */
    @Override
    public List<Region> selectRegionList(Region region) {
        return regionMapper.selectRegionList(region);
    }

    @Override
    public List<Region> listAll() {
        List<Region> list = list(new LambdaQueryWrapper<Region>().eq(Region::getLevel, 1));
        list.forEach(item -> {
            List<Region> list1 = list(new LambdaQueryWrapper<Region>().eq(Region::getParentCode, item.getCode()));
            list1.forEach(item1 -> {
                List<Region> list2 = list(new LambdaQueryWrapper<Region>().eq(Region::getParentCode, item1.getCode()));
                item1.setChildren(list2);
            });
            item.setChildren(list1);
        });
        return list;
    }


}
